#!/usr/bin/env python3
"""
过滤器功能自动化测试脚本
"""

import json
import sys
import requests
import time

def run_filter_tests():
    """运行过滤器测试"""
    print("开始运行过滤器功能测试...")
    
    # 加载测试用例
    with open('filter_test_cases.json', 'r', encoding='utf-8') as f:
        test_cases = json.load(f)
    
    base_url = 'http://localhost:5000'
    
    success_count = 0
    fail_count = 0
    
    for i, test_case in enumerate(test_cases, 1):
        print(f"\n=== 测试用例 {i}: {test_case['name']} ===")
        print(f"描述: {test_case['description']}")
        
        try:
            # 构建请求数据
            request_data = {
                'filters': test_case['filters']
            }
            if 'logic' in test_case:
                request_data['logic'] = test_case['logic']
            
            # 发送请求
            response = requests.get(f'{base_url}/statistics/chart_data', 
                                  params={'config': json.dumps(request_data)},
                                  timeout=10)
            
            if response.status_code == 200:
                data = response.json()
                print(f"✓ 测试通过 - 返回 {len(data)} 条记录")
                success_count += 1
                
                # 显示前3条结果作为验证
                for j, record in enumerate(data[:3]):
                    print(f"  记录 {j+1}: {record.get('symbol', 'N/A')} - {record.get('net_profit', 'N/A')}")
                    
            else:
                print(f"✗ 测试失败 - HTTP {response.status_code}: {response.text}")
                fail_count += 1
                
        except Exception as e:
            print(f"✗ 测试异常: {e}")
            fail_count += 1
        
        time.sleep(0.5)  # 避免请求过快
    
    print(f"\n=== 测试总结 ===")
    print(f"总测试数: {len(test_cases)}")
    print(f"成功: {success_count}")
    print(f"失败: {fail_count}")
    print(f"成功率: {success_count/len(test_cases)*100:.1f}%")

if __name__ == '__main__':
    run_filter_tests()
